
package com.yimixian.app.address;

import com.baidu.location.BDLocation;
import com.baidu.mapapi.model.LatLng;
import com.baidu.mapapi.search.core.PoiInfo;
import com.baidu.mapapi.search.geocode.GeoCoder;
import com.baidu.mapapi.search.geocode.ReverseGeoCodeResult;
import com.umeng.analytics.MobclickAgent;
import com.yimixian.app.EventBusDomain.HomeDataLocatedStatus;
import com.yimixian.app.R;
import com.yimixian.app.adapter.ChooseCityAdapter;
import com.yimixian.app.common.CacheKeys;
import com.yimixian.app.common.Constants;
import com.yimixian.app.common.SharePreferenceKeys;
import com.yimixian.app.common.StatConstants;
import com.yimixian.app.common.YmxTitleBarActivity;
import com.yimixian.app.data.DataManager;
import com.yimixian.app.location.LocatedListener;
import com.yimixian.app.location.LocationProvider;
import com.yimixian.app.model.AppConfig;
import com.yimixian.app.model.City;
import com.yimixian.app.model.Region;
import com.yimixian.app.util.SharedPreferencesHelper;
import com.yimixian.app.util.UiUtils;

import de.greenrobot.event.EventBus;
import rx.android.schedulers.AndroidSchedulers;
import rx.functions.Action1;
import rx.schedulers.Schedulers;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

import java.util.ArrayList;
import java.util.List;

public class ChooseCityActivity extends YmxTitleBarActivity {
    public static final String EXTRA_CITY_NAME = "extra_city_name";
    @InjectView(R.id.city_name_text)
    TextView mCityNameText;
    @InjectView(R.id.progress_bar)
    ProgressBar mProgressBar;
    @InjectView(R.id.list_view)
    ListView mListView;

    private DataManager mDataManager;
    private String mCityName;
    private String mLocationCityName;
    private String mCityId;
    private ArrayList<City> mCities = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mDataManager = DataManager.getInstance();
        AppConfig appConfig = mDataManager.get(CacheKeys.APP_CONFIG);
        mCities.clear();
        List<Region> regions = appConfig.regions;
        if (regions != null){
            for (Region mRegion : regions){
                if (mRegion.cities != null) mCities.addAll(mRegion.cities);
            }
        }
        setContentView(R.layout.choose_city_activity);
        ButterKnife.inject(this);

        String cityName = mDataManager.get(CacheKeys.CURRENT_POSITION_CITY);
        if (!TextUtils.isEmpty(cityName)) {
            mCityNameText.setText(cityName);
            mLocationCityName = cityName;
        }
        ChooseCityAdapter adapter = new ChooseCityAdapter(this);
        adapter.setList(mCities);
        mListView.setAdapter(adapter);
    }

    @OnClick({
            R.id.locate_text, R.id.locate_image
    })
    public void onLocationClick(View view) {
        MobclickAgent.onEvent(this, StatConstants.CHOOSE_CITY_GPS_CLICK);
        mCityNameText.setEnabled(false);
        mProgressBar.setVisibility(View.VISIBLE);
        mCityNameText.setText(getString(R.string.locating));

        LocationProvider.getInstance().startBaiduLocated(false, new LocatedListener() {

            @Override
            public void locatedSuccessListener(BDLocation location) {
                mCityNameText.setText(mDataManager
                        .<String> get(CacheKeys.CURRENT_POSITION_CITY));
                mProgressBar.setVisibility(View.GONE);
                mCityNameText.setEnabled(true);
            }

            @Override
            public void locatedSuccessGeoPoiInfoListener(PoiInfo poiInfo) {

            }

            @Override
            public void locatedFailedListener() {
                onLocatingFailed();
            }

            @Override
            public void locatedFailedGeoPoiInfoListener() {

            }
        });
    }

    @OnClick({
            R.id.city_name_text
    })
    public void onCityNameClick(View view) {
        mCityName = mLocationCityName;
        for (City city : mCities) {
            //此处为了匹配服务器返回不带 “市”
            String name = city.name;
            if (name == null || name.length() <= 0) return;
            if (!name.contains("市")) name = name + "市";
            if (city.name.equals(mCityName) || name.equals(mCityName)) {
                mCityId = city.id;
                break;
            }
        }
        returnWithCityName(mCityId, mCityName);
    }

    public void returnWithCityName(String regionId, String cityName) {
        mCityId = regionId;
        mCityName = cityName;
        Intent intent = new Intent();
        intent.putExtra(ChooseAddressOrStoreActivity.EXTRA_SELECTED_REGION_ID, regionId);
        intent.putExtra(ChooseCityActivity.EXTRA_CITY_NAME, cityName);
        setResult(RESULT_OK, intent);
        finish();
    }

    @Override
    protected String getTitleBarText() {
        return getString(R.string.choose_city);
    }

    private void onLocatingFailed() {
        UiUtils.showToast(this, getString(R.string.locate_fail));
        mCityNameText.setText(getString(R.string.locate_current_position));
        mProgressBar.setVisibility(View.INVISIBLE);
    }
}
